package com.wonders.module.mail.ws;

import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import com.wonders.module.mail.model.MailSendInfo;
import com.wonders.module.mail.model.User;
import com.wonders.module.mail.ws.modle.req.MailReqMsg;
import com.wonders.module.mail.ws.modle.res.Data;
import com.wonders.module.mail.ws.modle.res.MailResMsg;
import com.wonders.module.utils.SendMail;

/**
 * Created by myd on 2017/4/11.
 */
public class MailService {

    public Boolean sendMail(User user)
    {
        Boolean flag = false;
        try {

            flag = systemSendMail("青海省人力资源与社会保障厅", "myude@foxmail.com", "找回密码", "ip地址为:" + user.getUserip() + "用户名:" + user
                    .getName());
        }catch (Exception e)
        {
            e.getStackTrace();
        }
        return  flag;
    }
    public User xmlToModel(String xml)
    {
        User sbkModel = null;
        try {
            JAXBContext context = JAXBContext.newInstance(User.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            sbkModel = (User) unmarshaller.unmarshal(new StringReader(xml));
//            System.out.println("查询到的返回值为"+XMLFormatter.format(sbkModel.toString()));

        }catch (Exception e)
        {
            e.getStackTrace();
        }
        return sbkModel;
    }

    public MailResMsg MailSend(String xml)
    {
        MailResMsg mailResMsg = null;//返回的报文
        try {
            MailReqMsg mailreqmsg = null;//接收到的报文
            //转换接收到的xml为bean
            JAXBContext context = JAXBContext.newInstance(MailReqMsg.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            mailreqmsg = (MailReqMsg) unmarshaller.unmarshal(new StringReader(xml));
            if (mailreqmsg != null)
            {
                String idcard=mailreqmsg.getData().getIdcard();
                String username=mailreqmsg.getData().getName();
                String userip = mailreqmsg.getData().getUserip();
                User user = new User();
                user.setIdcard(idcard);
                user.setName(username);
                user.setUserip(userip);
                boolean res = sendMail(user);//发送邮件
                if (res)
                {
                    mailResMsg = new MailResMsg();
                    Data data = new Data();
                    //赋值
                    data.setState("发送成功");
                    mailResMsg.setHead(mailResMsg.getHead());
                    mailResMsg.setData(data);
                }
                else
                {
                    mailResMsg = new MailResMsg();
                    Data data = new Data();
                    //赋值
                    data.setState("发送失败");
                    mailResMsg.setHead(mailResMsg.getHead());
                    mailResMsg.setData(data);
                }
            }
        }
        catch (Exception e)
        {
            e.getStackTrace();
            mailResMsg = new MailResMsg();
            Data data = new Data();
            //赋值
            data.setState("发送异常");
            mailResMsg.setHead(mailResMsg.getHead());
            mailResMsg.setData(data);
        }

        return mailResMsg;
    }

    private boolean systemSendMail(String nicheng,String email,String title,String text) {

        boolean flag = false;
        try {
            MailSendInfo mailInfo = new MailSendInfo();
            mailInfo.setMailServerHost("smtp.163.com");
//            mailInfo.setMailServerHost("220.181.72.147");
            mailInfo.setValidate(true);
            mailInfo.setUsername("13897483797@163.com");
            mailInfo.setPassword("myd1991521");
            mailInfo.setFromaddress("13897483797@163.com");
            mailInfo.setToaddress(email);
            SendMail sendMail = new SendMail();
            flag =  sendMail.sendTextMail(mailInfo,nicheng,title,text);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }
}
